package com.pingtai.timer;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.pingtai.ajax.entity.IndentTarget_163;
import com.pingtai.ajax.service.NoticeService;
import com.pingtai.indent.entity.IndentNumberTmp;
import com.pingtai.indent.entity.UserIndent;
import com.pingtai.indent.service.WeiboIndentService;
import com.pingtai.user.service.UserManagerService;
import com.pingtai.xinlang.login.login.WeiboLoginService;

public class IndentAutoCompleteTimer_tx_persion_renzheng extends Thread  implements ApplicationContextAware{

	private int base;
	
	

	public NoticeService getNoticeService() {
		return noticeService;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	private  UserManagerService userManagerService;
	private  WeiboIndentService weiboIndentService;
	private WeiboLoginService xinlangWeiboLoginService; 
	private  NoticeService noticeService;
	private  ApplicationContext context;
	
	public WeiboLoginService getXinlangWeiboLoginService() {
		return xinlangWeiboLoginService;
	}

	public void setXinlangWeiboLoginService(
			WeiboLoginService xinlangWeiboLoginService) {
		this.xinlangWeiboLoginService = xinlangWeiboLoginService;
	}

	private static boolean flag = true;
	
	public void run(){
		//if(!flag) return;
		flag = false;
		if(userManagerService==null) {
			userManagerService = (UserManagerService) context.getBean("userManagerTimerServices");
		}
			
		if(weiboIndentService==null) {
			weiboIndentService = (WeiboIndentService) context.getBean("weiboIndentTimerServices");
		}
			
		if(xinlangWeiboLoginService==null){
			xinlangWeiboLoginService = (WeiboLoginService) context.getBean("xinlangWeiboLoginTimerService");
		}
		if(noticeService==null){
			noticeService = (NoticeService) context.getBean("noticeTimerServices");
		}
			
		List list = weiboIndentService.getAllRuningIndent(base);
		if(list == null || list.size()==0) {
			flag = true;
			return;
		}
		for(int m=0;m<list.size();m++){
			UserIndent userIndent = (UserIndent) list.get(m);
			String url = userIndent.getRenzheng().getTencentPersonRenzheng().getUrl();
			try {
				boolean isRenzheng = noticeService.checkRenzheng_tx_persion(url);
				
				if(isRenzheng){
					userIndent.setState(2);
					userIndent.getRenzheng().getTencentPersonRenzheng().setState(2);
					weiboIndentService.updateUserIndent(userIndent);
				}
				
			} catch (Exception e) {
				if((m+1)==list.size()){
					flag = true;
					return;
				}
				e.printStackTrace();
				continue;
			}
		}
		flag = true;
	}

	
	public UserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	public WeiboIndentService getWeiboIndentService() {
		return weiboIndentService;
	}

	public void setWeiboIndentService(WeiboIndentService weiboIndentService) {
		this.weiboIndentService = weiboIndentService;
	}


	
	public int getBase() {
		return base;
	}

	public void setBase(int base) {
		this.base = base;
	}

	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		this.context = arg0;
		
	}
}
